package org.example.car.service.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import com.alibaba.excel.util.ListUtils;
import lombok.extern.slf4j.Slf4j;
import org.example.car.service.domain.Types;
import org.example.car.service.service.TypesService;

import java.util.List;


/**
 * @Author: Yanyanyu
 * @Date: 2024/6/30 20:41
 */
@Slf4j
public class TypesListener implements ReadListener<Types> {

    private static final int BATCH_COUNT = 3;

    private List<Types> typesList= ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);


    private TypesService typesService;

    public TypesListener(TypesService typesService){
        this.typesService=typesService;
    }


    @Override
    public void invoke(Types types, AnalysisContext analysisContext) {
        System.out.println("解析的数据"+types);

        typesList.add(types);

        if(typesList.size()>BATCH_COUNT){
            typesService.insertAll(typesList);

            typesList=ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

        System.out.println("解析成功");
        if(typesList.size()>0){
            typesService.insertAll(typesList);
        }
    }
}
